<?php

namespace addons\sum\service\user;

use app\admin\model\sum\user\Coupon as UserCouponModel;
use app\admin\model\sum\user\User as UserModel;

class Coupon
{
    /**
     * 购物赠送
     * @return void
     */
    public static function shopping_give($user)
    {
        // 判断用户
        if (is_numeric($user)) {
            $user = UserModel::getById($user);
        }
        if (!$user) {
            error_stop('未找到用户');
        }
        //获取优惠券配置
        $couponConfig = sum_config('sum.coupon');
        $num=$couponConfig['num'];
        //要赠送的劵数据
        $sends = [];
        if($num>0){
            for ($i=0;$i<$num;$i++){
                $current = [
                    'user_id' => $user['id'],
                    'use_time' => null,
                    'createtime' => time(),
                    'updatetime' => time(),
                    'name' => $couponConfig['name'],
                    'description' => $couponConfig['description'],
                    'amount' => $couponConfig['amount'],
                    'enough' => $couponConfig['enough'],
                    'use_end_time' => time()+($couponConfig['days'] * 86400),//可使用日期
                    'oper_id' => 0,//来自系统赠送
                    'send_time' => null,//赠送给好友，好友接收时间
                ];
                $sends[] = $current;
            }
            UserCouponModel::insertAll($sends);
        }
        return true;
    }



}
